var that;
var app = getApp();
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({ 
      is_hidd: true 
    }) 
    countdown = 60;
    return;
  } else { 
    that.setData({
      is_hidd: false,
      last_time: countdown
    })
    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }, 1000)
}

Page({
  data: {
    picker1Value: 0,
    picker1Range: ['+86', '+80', '+84', '+87'],
    phone: "",
    is_hidd: true,
    last_time: '',
    pwd:'',
    pwd2:'', 
    code:'',//被别人邀请的邀请码
    code_y:'',//验证码
    has_phone:false,
    url:"./invite_success",
    hid_pw1:true,
    hid_pw2: true,
    change_pwd: false,
    disable:false,  //是否禁用 
    realname:'',
    r:false
  },
  onLoad: function (options) { 
    that=this;
    console.log(options)
    that.check_has_phone() 
    if (options.from&&options.from=="p_center"){   //从个人中心申请会员跳入注册,
      that.setData({ f: options.from, change_pwd: true,r:true,url:"../member/buy_cart"}) 
    }
    if (options.code) {   //被别人邀请进入注册会员
      that.setData({ code: options.code})
    }
    if (options.from && options.from == "change_pwd") { //修改密码
      that.setData({ f: options.from, change_pwd: true,disable:true})
      wx.setNavigationBarTitle({  title: '修改密码' })
    }
  },

  onReady: function () {
  
  },
  onShow: function () {
  
  },
  img1:function(){
    this.setData({ hid_pw1: !that.data.hid_pw1 })
  },
  img2: function () {
    this.setData({ hid_pw2: !that.data.hid_pw2 })
  },
  pwdInput:function(e){
    this.setData({ pwd: e.detail.value.trim() })
  },
  pwdInput2: function (e) {
    this.setData({ pwd2: e.detail.value.trim() })
  },
  nameInput: function (e) {
    this.setData({ realname: e.detail.value.trim() })
  },
  suc: function () {
    if (that.data.pwd == '' || that.data.pwd2 == ' '||that.data.pwd.length!=6||that.data.pwd!= that.data.pwd2){
      app.toast_up('格式有误')
      return;
    } 
    
    // if ((that.data.realname == '' || that.data.realname == ' ') && that.data.r==true){
    //   app.toast_up('姓名有误！')
    // }
    console.log(that.data.realname); 
    if (that.data.realname != '' &&that.data.realname != ' '){
      app.update_person_msg("custom_nickname", that.data.realname);
      app.globalData.userInfo.custom_nickname = that.data.realname;
    }
    app.req("regist_pwd.html", { "openid": app.data.openid, 'pwd': that.data.pwd, code: that.data.code, only_cp: that.data.change_pwd, 'name': that.data.realname }, function (res) {
      if (res.status == "success") {
        app.toast('设置成功')
        setTimeout(function () {
                                                       //只修改密码成功跳回
          if (that.data.change_pwd && that.data.f == "change_pwd"){ wx.navigateBack({ delta: 1 });return} 
          
          wx.redirectTo({ url: that.data.url }) //用户名密码设置成功，跳到指定界面
        }, 2000) //延迟时间 这里是1秒
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.suc()
          }
        })
      }
    })
  },
  check_has_phone:function(){
    app.getUserInfo(function(){
      app.req("check_has_phone.html", { "openid": app.data.openid }, function (res) {
        if (res.status == "success") {
          if (that.data.change_pwd && that.data.f =="change_pwd") { that.setData({ has_phone: false, phone: res.mess2.mobile }); return }
          that.setData({ has_phone: res.mess });
        } else if (res.status == "error") {
          app.toast_up(res.mess)
        } else {
          app.modal("错误提示", "网络错误，是否重试？", function (re) {
            if (re) {
              that.check_has_phone()
            }
          })
        }
      })
    })
    
  },
  area_code: function (e) {//点击+86选择区号
  },
  normalPickerBindchange: function (e) {
    this.setData({
      picker1Value: e.detail.value
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      phone: e.detail.value.trim()
    })
  }, 
  NameInput: function (e) {
    this.setData({
      code_y: e.detail.value
    })
  },
  get_code: function () {  //获取验证吗
    if (that.data.f == "change_pwd" && that.data.phone != app.globalData.userInfo.phone) {
      app.toast_up("号码有误")   //修改密码时判断手机号是否为本人手机，否则不发验证吗
      return
    }
    app.req("sendmess.html", { "openid": app.data.openid, phone: that.data.phone }, function (res) {
      if (res.status == "success") {
        // 将获取验证码按钮隐藏60s，60s后再次显示
        that.setData({ is_hidd: (!that.data.is_hidd) })
        settime(that);
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_code()
          }
        })
      } 
    })
  },
  name_ok: function () {   //验证验证吗 
    // that.setData({ has_phone: !(that.data.has_phone) }); 
    // return;
    app.req("change_phone.html", { "openid": app.data.openid, "phone": that.data.phone, "icode": that.data.code_y }, function (res) {
      if (res.status == "success") {
        app.toast(res.mess)
        that.setData({ has_phone: !(that.data.has_phone)});
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.name_ok()
          }
        })
      }
    })
  }
})